<template>
  <div class="login-form">
    <div v-if="!isLoggedIn && !isResetting" class="login-form__inner">
      <h2 class="login-title">用户登录</h2>
      <input v-model="username" placeholder="请输入用户名" class="login-input"/>
      <input v-model="password" type="password" placeholder="请输入密码" class="login-input"/>
      <button @click="login" class="login-btn">登录</button>
    </div>
    <div style="margin:20px 0">
      沪ICP备2023014104号-1
    </div>
    <div style="width:300px;margin:0 auto; padding:20px 0;">
      <a target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=31010602007542" style="display:inline-block;text-decoration:none;height:20px;line-height:20px;"><img src="../assets/img.png" style="float:left;"/><p style="float:left;height:20px;line-height:20px;margin: 0px 0px 0px 5px; color:#939393;">沪公网安备 31010602007542号</p></a>
    </div>

  </div>
</template>

<script>
import md5 from "js-md5";
import axios from "axios";
import http from "../request/http.js";
import router from "../router/router.js";

export default {
  name: "LoginPage",
  data() {
    return {
      username: '',
      password: '',
    }
  },
  methods: {
    async login() {
      const md5Pwd = md5(this.password);
      const res = await http({
        url: '/api/loginManager',
        method: 'post',
        data: {
          username: this.username,
          password: md5Pwd,
        }
      })
      console.log(res)
      if (res.success == true && res.data.token) {
        // 登录成功，保存 token 并显示三个按钮
        localStorage.setItem('token', res.data.token);
        router.push('/main');
      } else {
        // 登录失败，提示错误信息
        alert('用户名和密码错误');
      }
    }
  }
}
</script>

<style scoped>
.login-form {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.login-form__inner {
  background-color: white;
  padding: 30px;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  text-align: center;
  animation: fadeIn 0.5s ease-in-out;
}

.login-title {
  margin-top: 0;
  margin-bottom: 20px;
}

.login-input {
  display: block;
  width: 100%;
  height: 40px;
  padding: 10px;
  margin-bottom: 20px;
  font-size: 16px;
  border-radius: 5px;
  border: 1px solid #ccc;
}

.login-btn {
  display: block;
  width: 100%;
  height: 40px;
  padding: 10px;
  margin-top: 20px;
  font-size: 16px;
  background-color: #2196f3;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: background-color 0.2s ease-in-out;
}

.login-btn:hover {
  background-color: #1976d2;
}
</style>
